package cn.nicklemay.interview.algorithm;

public class MoreThanHalfNum_Solution
{
    public static int MoreThanHalfNum(int [] array) {
        int max = 0;
        for (int i = 0; i < array.length; i++)
        {
            max = array[i] > max ? array[i] : max;
        }
        int[] number = new int[max - 1];
        for (int i = 0; i < array.length; i++)
        {
            number[array[i]]++;
        }
        int res = 0;
        int x = 0;
        for (int j =0 ;j < number.length; j++)
        {
            if (res < number[j])
            {
                res = number[j];
                x = j;
            }
        }
        if (res * 2 > array.length)
        {
            return x;
        } else
        {
            return 0;
        }
    }

    public static void main(String[] args)
    {
        int[] a = {1,2,3,2,2,2,5,4,2};
        int res = MoreThanHalfNum(a);
    }
}
